﻿using Microsoft.EntityFrameworkCore;
using PdfSharp;
using System;
using WidgetsDesign.API.DataBase;
using WidgetsDesign.API.Models;

namespace WidgetsDesign.API.Services
{
    public class RiskRepositoryRepository : IRiskRepositoryRepository
    {
        private readonly AppDbContext _context;
        public RiskRepositoryRepository(AppDbContext context)
        {
            _context = context;
        }
        public async Task<IEnumerable<RiskRepositoryModel>> GetRiskRepositoryListAsync(string type, int pageIndex, int pageSize)
        {
            var query = (from h in _context.RiskRepository
                             /*where types.Contains(h.Type) && h.Length >= MinSize && h.Length <= MaxSize*/
                         select h).Skip(pageSize * (pageIndex-1)).Take(pageSize);
            var filteredData = await query.ToListAsync();
            return filteredData;
        }
        public int GetRiskRepositoryListTotal()
        {
            return (from h in _context.RiskRepository
                    select h).Count();
        }

        public void AddRiskRepository(RiskRepositoryModel RiskRepository)
        {
            if (RiskRepository == null)
            {
                throw new ArgumentNullException(nameof(RiskRepository));
            }
            _context.RiskRepository.Add(RiskRepository);
        }

        public async Task<bool> SaveAsync()
        {
            return (await _context.SaveChangesAsync() >= 0);
        }

        public async Task<RiskRepositoryModel> GetRiskRepositoryAsync(string id)
        {
            var query = (from h in _context.RiskRepository
                         where h.Id == id
                         select h);
            var filteredData = await query.FirstOrDefaultAsync();
            return filteredData;
        }
        public void UpdateRiskRepository(RiskRepositoryModel model)
        {
            var query = (from h in _context.RiskRepository
                         where h.Id == model.Id
                         select h);
           var info =  query.FirstOrDefault();
            if(info != null)
            {
                info.Update_time = model.Update_time;
                info.Work_task = model.Work_task;
                info.Unit = model.Unit; 
                info.Coding = model.Coding;
                info.Workflow = model.Workflow;
                info.Time = model.Time;
                info.Group_member = model.Group_member;
                info.Step_details = model.Step_details;
                info.Location = model.Location;

            }
            _context.SaveChanges();
        }
        /* 
         public async Task<bool> GetHitokotoExistsAsync(int HitokotoId)
         {
             return await _context.Hitokoto.AnyAsync(t => t.Id == HitokotoId);
         }
 
        */
    }
}
